﻿Imports System.Activities
Imports System.Management
Imports System.Activities.Presentation.Metadata
Imports System.Drawing
Imports System.ComponentModel
Imports System.Activities.Presentation

Public Class WMICreateScopeActivity
    Inherits CodeActivity
    Implements IRegisterMetadata

    Public Property Scope As InOutArgument(Of ManagementScope)
    Public Property Namespaces As InArgument(Of String) = "root\cimv2"
    Public Property HostName As InArgument(Of String)
    Public Property Username As InArgument(Of String)
    Public Property Password As InArgument(Of String)

    Public Sub New()
        Me.DisplayName = My.Resources.WMICreateScopeDisplayName
    End Sub

    Protected Overrides Sub Execute(context As System.Activities.CodeActivityContext)
        Dim target = context.GetValue(Me.Scope)
        Dim nm = context.GetValue(Me.Namespaces)
        Dim host = context.GetValue(Me.HostName)
        Dim user = context.GetValue(Me.Username)
        Dim pass = context.GetValue(Me.Password)

        Dim wmConnectOption As ConnectionOptions = Nothing
        If host <> My.Computer.Name Then
            wmConnectOption = New ConnectionOptions With {
                .Impersonation = ImpersonationLevel.Impersonate,
                .EnablePrivileges = True,
                .Username = user,
                .Password = pass
                }
        End If
        If wmConnectOption Is Nothing Then
            target = New ManagementScope("\\" + host + "\" + nm)
        Else
            target = New ManagementScope("\\" + host + "\" + nm, wmConnectOption)
        End If

        context.SetValue(Me.Scope, target)
    End Sub

    Public Overridable Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'WMICreateScopeActivity Activity
        build.AddCustomAttributes(GetType(WMICreateScopeActivity),
                                              New DesignerAttribute(GetType(WMIDefaultActivityDesigner), GetType(ActivityDesigner)),
                                              New ToolboxBitmapAttribute(GetType(WMICreateScopeActivity), "WmiDefault.png"))

        build.AddCustomAttributes(GetType(WMICreateScopeActivity), "DisplayName",
                                      New CategoryAttribute(My.Resources.CategorySetting),
                                      New DisplayNameAttribute(My.Resources.DisplayNameDisplay))

        build.AddCustomAttributes(GetType(WMICreateScopeActivity), "Scope",
                                      New CategoryAttribute(My.Resources.CategoryDetail),
                                      New DisplayNameAttribute(My.Resources.ScopeDisplay),
                                      New DescriptionAttribute(My.Resources.ScopeDescription))
        build.AddCustomAttributes(GetType(WMICreateScopeActivity), "Namespaces",
                                      New CategoryAttribute(My.Resources.CategoryDetail),
                                      New RequiredArgumentAttribute(),
                                      New DisplayNameAttribute(My.Resources.NamespacesDisplay),
                                      New DescriptionAttribute(My.Resources.NamespacesDescription))
        build.AddCustomAttributes(GetType(WMICreateScopeActivity), "HostName",
                                      New CategoryAttribute(My.Resources.CategoryDetail),
                                      New DisplayNameAttribute(My.Resources.HostNameDisplay),
                                      New DescriptionAttribute(My.Resources.HostNameDescription))
        build.AddCustomAttributes(GetType(WMICreateScopeActivity), "Username",
                                      New CategoryAttribute(My.Resources.CategoryDetail),
                                      New DisplayNameAttribute(My.Resources.UsernameDisplay),
                                      New DescriptionAttribute(My.Resources.UsernameDescription))
        build.AddCustomAttributes(GetType(WMICreateScopeActivity), "Password",
                                      New CategoryAttribute(My.Resources.CategoryDetail),
                                      New DisplayNameAttribute(My.Resources.PasswordDisplay),
                                      New DescriptionAttribute(My.Resources.PasswordDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)

    End Sub
End Class
